package main

// import "fmt"
import . "nc_tools"

/*
 * type ListNode struct{
 *   Val int
 *   Next *ListNode
 * }
 */

/**
 * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
 *
 *
 * @param head1 ListNode类
 * @param head2 ListNode类
 * @return ListNode类
 */
func ReverseList(head *ListNode) *ListNode {
	// write code here
	var pre *ListNode = nil
	cur := head
	for cur != nil {
		tmp := cur.Next
		cur.Next = pre
		pre = cur
		cur = tmp
	}
	return pre
}
func addInList(head1 *ListNode, head2 *ListNode) *ListNode {
	// write code here
	h1 := ReverseList(head1)
	h2 := ReverseList(head2)
	s := 0
	var ans *ListNode = &ListNode{-1, nil}
	node := ans
	for h1 != nil || h2 != nil {
		value1 := 0
		value2 := 0
		if h1 != nil {
			value1 = h1.Val
			h1 = h1.Next
		}
		if h2 != nil {
			value2 = h2.Val
			h2 = h2.Next
		}
		sum := s + value1 + value2
		node.Next = &ListNode{sum % 10, nil}
		s = sum / 10
		node = node.Next
	}
	if s > 0 {
		node.Next = &ListNode{1, nil}
	}
	return ReverseList(ans.Next)
}
